<?php	
	require_once('start_session.php');
	require_once('db_connect.php');	
	require_once 'xpm/MAIL.php';	
				
	$member_id = $_COOKIE['user_id'];
	$title = $_POST['title'];
	$category = $_POST['category'];
	$keyword1 = $_POST['keyword1'];
	$keyword2 = $_POST['keyword2'];
	$start_date = $_POST['start_date'];
	$end_date = $_POST['end_date'];
	$start_time_1 = $_POST['start_time_1'];
	$start_time_2 = $_POST['start_time_2'];
	$end_time_1 = $_POST['end_time_1'];
	$end_time_2 = $_POST['end_time_2'];
	$require_login = $_POST['member_confirm'];
	$mid_result = $_POST['result_open'];
	$max_capacity = $_POST['max_capacity'];
	$locatename = $_POST['locatename'];
	$locatename2 = $_POST['locatename2'];
	$is_once = $_POST['is_once'];
	
	
	//print_r($_POST);
	
	$question=array();
	$question_du=array();
	$item=array();
	//기본조건 관련 변수
	$age_cond=array();
	$gender_cond=array();
	$edulevel_cond=array();
	$locatename_cond=array();
	
	for($i=1; $i<100; $i++)
	{
		$question[$i] = $_POST['question_'.$i];	
		$question_du[$i] = $_POST['question_'.$i.'_du'];
		$age_cond[$i] = $_POST['age_cond_'.$i];
		$gender_cond[$i] = $_POST['gender_cond_'.$i];
		$edulevel_cond[$i] = $_POST['edulevel_cond_'.$i];
		$locatename_cond[$i] = $_POST['locatename_cond_'.$i];
				
		for($j=1; $j<100; $j++)
		{
			$item[$i][$j] = $_POST['item_'.$i.'_'.$j];
		}
	}	
	
	$start_time = $start_time_1.':'.$start_time_2;
	$end_time = $end_time_1.':'.$end_time_2;
	$keyword = $keyword1.','.$keyword2;
	
	$sql = "SELECT ap_id FROM `standard_ap` WHERE `locatename` = '$locatename'";
	$result = mysqli_query($conn, $sql);
	if($result)
	{
		$row=mysqli_fetch_array($result);
		$ap_id = $row[ap_id];
	}	
	
	
	
	$sql = "SELECT gps_id FROM `standard_gps` WHERE `locatename` = '$locatename2'";
	$result = mysqli_query($conn, $sql);	
	if($result)
	{
		$row=mysqli_fetch_array($result);
		$gps_id = $row[gps_id];
	}	
	
		if($ap_id && $gps_id)
		{
		$sql="INSERT INTO `poll_system`.`survey` (`id`,`title` ,`is_once` ,`start_date` ,`end_date` ,`start_time` ,`end_time` ,`max_capacity` ,`require_login`,
											`mid_result`, `ap_id` ,`gps_id` ,`category` ,`member_id` ,`view_count`,`create_time`)
			VALUES ('$survey_id', '$title', '$is_once', '$start_date', '$end_date', '$start_time', '$end_time', '$max_capacity', '$require_login', '$mid_result', '$ap_id',
				'$gps_id', '$category', '$member_id', '$view_count',now());";		
		}
		else if($ap_id && !$gps_id)
		{
		$sql="INSERT INTO `poll_system`.`survey` (`id`,`title` ,`is_once` ,`start_date` ,`end_date` ,`start_time` ,`end_time` ,`max_capacity` ,`require_login`,
											`mid_result`, `ap_id` ,`category` ,`member_id` ,`view_count`,`create_time`)
			VALUES ('$survey_id', '$title', '$is_once', '$start_date', '$end_date', '$start_time', '$end_time', '$max_capacity', '$require_login', '$mid_result', '$ap_id',
				'$category', '$member_id', '$view_count',now());";
		}
		else if($gps_id && !$ap_id)
		{
			$sql="INSERT INTO `poll_system`.`survey` (`id`,`title` ,`is_once` ,`start_date` ,`end_date` ,`start_time` ,`end_time` ,`max_capacity` ,`require_login`,
											`mid_result`, `gps_id` ,`category` ,`member_id` ,`view_count`,`create_time`)
			VALUES ('$survey_id', '$title', '$is_once', '$start_date', '$end_date', '$start_time', '$end_time', '$max_capacity', '$require_login', '$mid_result',
				'$gps_id', '$category', '$member_id', '$view_count',now());";
		}
		else
		{
			$sql="INSERT INTO `poll_system`.`survey` (`id`,`title` ,`is_once` ,`start_date` ,`end_date` ,`start_time` ,`end_time` ,`max_capacity` ,`require_login`,
											`mid_result`, `category` ,`member_id` ,`view_count`, `create_time`)
			VALUES ('$survey_id', '$title', '$is_once', '$start_date', '$end_date', '$start_time', '$end_time', '$max_capacity', '$require_login', '$mid_result', 
			'$category', '$member_id', '$view_count',now());";
		}
		
		$result= mysqli_query($conn, $sql);
		
	
	/*
	if($survey_id==0)
	{
		$sql="SELECT id FROM `survey` WHERE 1 ORDER BY id DESC LIMIT 1 ";
		$result= mysqli_query($conn, $sql);
		$row = mysqli_fetch_array($result);
		$survey_id = $row[id];
		
		//새로만들어 지는 설문에 해당하는 카테고리/키워드 를 관심 카테고리/키워드로 가지는 회원들에게 메일을 보냄
		
		
		$tok = strtok($keyword, ",");
		while($tok!='')
		{
			$sql="INSERT INTO keyword_survey (`survey_id`, `keyword`) VALUES ('$survey_id','$tok');";
			mysqli_query($conn, $sql);
			$tok = strtok(",");
		}
		
		//새로만들어 지는 설문에 해당하는 카테고리/키워드 를 관심 카테고리/키워드로 가지는 회원들에게 메일을 보냄
		$getemail_query = "(SELECT email, member.id FROM member, category_mem, survey WHERE survey.id = '$survey_id' AND category_mem.mem_id = member.id AND survey.category = category_mem.category AND member.id not in (select id from session))
							union (select email, member.id from survey, keyword_mem, keyword_survey, member where survey.id = '$survey_id' and survey.id = keyword_survey.survey_id and keyword_survey.keyword = keyword_mem.keyword and keyword_mem.mem_id = member.id AND member.id not in (select id from session))";
							
		echo $getemail_query;
		$getemail_query_result = mysqli_query($conn, $getemail_query);
		while($getemail_row = mysqli_fetch_array($getemail_query_result))
		{
			echo $getemail_row[email]."<br>";
			$email = $getemail_row[email];
			$m= new MAIL;
			
			$m->From('aeraim@gmail.com');
			$m->AddTo($email);
			$m->Subject('관심(카테고리/키워드) 설문이 등록되었습니다. 많은 참여 부탁드립니다.');
			$m->Html("<b>새로등록된 설문</b> : $row[title]");
			
			$c = $m->Connect('smtp.gmail.com', 465, 'aeraim@gmail.com', 'famous05', 'tls', 10, 'localhost', null, 'plain') or die(print_r($m->Result));
			
			echo $m->Send($c) ? 'Mail Sent !' : 'Error !';
			
			$m->Disconnect();

			echo '<br /><pre>';
			
			print_r($m->History);
			
			list($tm1, $ar1) = each($m->History[0]);
			list($tm2, $ar2) = each($m->History[count($m->History)-1]);
			echo 'The process took : '.(floatval($tm2)-floatval($tm1)).' second.</pre>';

		}		
	}
	*/
	
	$sql="SELECT id FROM `survey` WHERE 1 ORDER BY id DESC LIMIT 1 ";
		$result= mysqli_query($conn, $sql);
		$row = mysqli_fetch_array($result);
		$survey_id = $row[id];
		
	$tok = strtok($keyword, ",");
	while($tok!='')
	{
		$sql="INSERT INTO keyword_survey (`survey_id`, `keyword`) VALUES ('$survey_id','$tok');";
		mysqli_query($conn, $sql);
		$tok = strtok(",");
	}
	
	for($i=1; $i<100; $i++)
	{
		if($question[$i])
		{
			$sql="INSERT INTO question (`survey_id` ,`qnumber` ,`qtitle` ,`is_multi`) VALUES ('$survey_id','$i', '$question[$i]','$question_du[$i]');";
			mysqli_query($conn, $sql);
			for($j=1; $j<100; $j++)
			{
				if($item[$i][$j])
				{
					$ititle = $item[$i][$j];
					$sql = "INSERT INTO item (`survey_id` ,`qnum` ,`inum` ,`ititle`) VALUES ('$survey_id','$i', '$j','$ititle');";
					mysqli_query($conn, $sql);
				}
			}
		}		
	}	
	for($i=1; $i<100; $i++)
	{
		if($age_cond[$i] || $gender_cond[$i] || $edulevel_cond[$i] || $addr_cond[$i])
		{
			if($_POST['gender_cond_'.$i]==NULL)
			{	
				$sql="INSERT INTO basic_condition (`survey_id`, `age`, `edulevel`, `addr`) VALUES ('$survey_id', '$age_cond[$i]',
			 '$edulevel_cond[$i]',  '$locatename_cond[$i]');";
			}
			else
			{
				$sql="INSERT INTO basic_condition (`survey_id`, `age`, `edulevel`, `gender`, `addr`) VALUES ('$survey_id', '$age_cond[$i]', '$edulevel_cond[$i]',
			 '$gender_cond[$i]',  '$locatename_cond[$i]');";
			}
			 mysqli_query($conn, $sql);
		}		
	}
	
	$sql="SELECT make_count FROM member WHERE member.id='$member_id'";
	$result=mysqli_query($conn, $sql);
	$row=mysqli_fetch_array($result);
	$make_count = $row[make_count] + 1;	
	
	$sql="UPDATE member SET make_count=$make_count WHERE member.id='$member_id'";
	mysqli_query($conn, $sql);
	
    //echo("
    //      <script>
    //      top.location.href='main.php';
    //      </script>
    //      ");
	
	//테스트
	/*
	for($i=0; $i<100; $i++)
	{		
		echo $question[$i];
		
		for($j=0; $j<100; $j++)
			echo $item[$i][$j];		
	}
	*/	
	
		
		//include "db_connect.php";	
		
		//이동
		//Header('Location:main.php');
		
	
	echo("
		<script>
		top.location.href='main.php';
		</script>
    ");	
		
		
		
		
	
?>
		
		
		
		
		
		

		
		